/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nnet2.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author kalinskia
 */
public class PlotPanel extends JPanel{
    private ArrayList<Double> real;
    private ArrayList<Double> forcast;
    private int iteration = -1;
    private double error = Double.NaN;

    public PlotPanel(ArrayList<Double> real, ArrayList<Double> forcast){
        this.real = real;
        this.forcast = forcast;
        this.setPreferredSize(new Dimension(800, 300));
    }

    @Override
    protected void paintComponent(Graphics g) {
        int len = this.getReal().size();
        int step = this.getWidth()/len;

        g.setColor(Color.white);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());

        g.setColor(Color.BLUE);
        if(this.iteration > -1) g.drawString(String.format("Iteration: %d", this.iteration), 10, 10);
        if(this.error != Double.NaN) g.drawString(String.format("Iteration: %f", this.error), 10, 20);

        g.setColor(Color.DARK_GRAY);
        g.drawLine(0, this.getHeight()>>1, this.getWidth(), this.getHeight()>>1);
        g.setColor(Color.LIGHT_GRAY);
        g.drawLine(0, this.getHeight()>>2, this.getWidth(), this.getHeight()>>2);
        g.drawLine(0, 3 * this.getHeight()>>2, this.getWidth(), 3 * this.getHeight()>>2);

        for(int i = 0; i < this.getReal().size();i++){
            g.setColor(Color.BLACK);
            int x = i * step;
            int x2 = x + step;
            g.fillRect(x-1, realVal(i)-1,3, 3);
            g.drawLine(x, realVal(i), x2, realVal(i+1));
            g.setColor(Color.RED);
            g.drawLine(x, foreVal(i), x2, foreVal(i+1));
            g.fillRect(x-1, foreVal(i)-1, 3, 3);
        }
    }

    private int foreVal(int i) {
        if(i >= this.getForcast().size()) return foreVal(i-1);
        return this.getHeight() - ((int) (this.getForcast().get(i) * this.getHeight() / 2) + this.getHeight() / 2);
    }

    private int realVal(int i) {
        if(i >= this.getReal().size()) return realVal(i-1);
        return this.getHeight() - ((int) (this.getReal().get(i) * this.getHeight() / 2) + this.getHeight() / 2);
    }

    public static PlotPanel showPlot(ArrayList<Double> real, ArrayList<Double> forcast){
        JFrame f = new JFrame();
        final PlotPanel plotPanel = new PlotPanel(real, forcast);
        f.setContentPane(plotPanel);
        f.setSize(800, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setDefaultLookAndFeelDecorated(true);

        //f.pack();
        f.setVisible(true);
        return plotPanel;
    }

    /**
     * @return the real
     */
    public ArrayList<Double> getReal() {
        return real;
    }

    /**
     * @param real the real to set
     */
    public void setReal(ArrayList<Double> real) {
        this.real = real;
    }

    /**
     * @return the forcast
     */
    public ArrayList<Double> getForcast() {
        return forcast;
    }

    /**
     * @param forcast the forcast to set
     */
    public void setForcast(ArrayList<Double> forcast) {
        this.forcast = forcast;
    }

    /**
     * @return the iteration
     */
    public int getIteration() {
        return iteration;
    }

    /**
     * @param iteration the iteration to set
     */
    public void setIteration(int iteration) {
        this.iteration = iteration;
    }

    /**
     * @return the error
     */
    public double getError() {
        return error;
    }

    /**
     * @param error the error to set
     */
    public void setError(double error) {
        this.error = error;
    }
}
